<template>
  <Modal
    title="编辑"
    :value="value"
    :mask-closable="true"
    :footer-hide="true"
    @input="$emit('input', $event)"
  >
    <Form
      ref="formLkyTeacherClass"
      :model="formLkyTeacherClass"
      :rules="ruleValidate"
      label-position="right"
      :label-width="80"
    >
      <FormItem label="姓名" prop="name">
        <Input type="text" v-model="formLkyTeacherClass.name" placeholder="姓名"/>
      </FormItem>
      <FormItem label="联系电话" prop="telphone">
        <Input type="text" v-model="formLkyTeacherClass.telphone" placeholder="联系电话"/>
      </FormItem>
      <FormItem label="邮箱" prop="email">
        <Input type="text" v-model="formLkyTeacherClass.email" placeholder="邮箱"/>
      </FormItem>
      <FormItem label="籍贯" prop="native_place">
        <Input type="text" v-model="formLkyTeacherClass.native_place" placeholder="籍贯"/>
      </FormItem>
      <FormItem label="备注信息" prop="remark_info">
        <Input type="text" v-model="formLkyTeacherClass.remark_info" placeholder="备注信息"/>
      </FormItem>
      <FormItem label="工号" prop="job_number">
        <Input type="text" v-model="formLkyTeacherClass.job_number" placeholder="工号"/>
      </FormItem>
      <FormItem>
        <Button type="primary" @click="handleSubmit('formLkyTeacherClass')">提交</Button>
        <Button @click="handleReset('formLkyTeacherClass')" style="margin-left: 8px">重置</Button>
      </FormItem>
    </Form>
  </Modal>
</template>
<script>
import { update } from '@/api/lky/lkyTeacherClass'
import { createModelObj, coverProps, coverFormProps } from '@/libs/util'

export default {
  name: 'Edit',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    lkyTeacher: {
      type: Object,
      default: null
    }
  },
  data () {
    return {
      formLkyTeacherClass: {
        name: '',
        telphone: 0,
        email: '',
        native_place: '',
        remark_info: '',
        job_number: ''
      },
      ruleValidate: {

        name: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 32, message: '长度最长为32', trigger: 'blur' }
        ],
        telphone: [
          { type: 'string', max: 12, message: '电话号码最长为32', trigger: 'blur' }
        ],
        email: [
          { required: false, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 50, message: '长度最长为50', trigger: 'blur' }
        ],
        native_place: [
          { required: false, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 50, message: '长度最长为50', trigger: 'blur' }
        ],
        remark_info: [
          { required: false, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 50, message: '长度最长为50', trigger: 'blur' }
        ],
        job_number: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 20, message: '长度最长为20', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleSubmit (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          update(
            Object.assign({}, createModelObj(this.formLkyTeacherClass, 'lkyTeacher'))
          ).then(res => {
            if (res) {
              this.$emit('updateRow', coverProps(this.lkyTeacher, this.formLkyTeacherClass))
              this.$emit('input', false)
              this.handleReset(name)
            }
          })
        } else {
          this.$Message.error('验证失败!')
        }
      })
    },
    handleReset (name) {
      this.$refs[name].resetFields()
    }
  },
  watch: {
    value: function (val, oldVal) {
      this.$refs.formLkyTeacherClass.resetFields()
      coverFormProps(this.formLkyTeacherClass, this.lkyTeacher)
    }
  }
}
</script>
